package hongvt.quanlynhanvien;

import java.util.List;

import hongvt.quanlynhanvien.database.DatabaseManager;
import hongvt.quanlynhanvien.model.Employee;
import hongvt.quanlynhanvien.model.WorkingMode;
import hongvt.quanlynhanvien.utils.Define;
import hongvt.quanlynhanvien.utils.StringUtils;
import android.widget.RadioGroup;
import android.widget.TextView;
import butterknife.InjectView;
import butterknife.OnClick;

public class EmployeeDetailActivity extends AbstractActivity {
	@InjectView(R.id.tv_name)
	TextView fullnameEdt;

	@InjectView(R.id.tv_code)
	TextView codeEdt;

	@InjectView(R.id.tv_phone)
	TextView phoneEdt;

	@InjectView(R.id.tv_salary)
	TextView salaryEdt;

	@InjectView(R.id.tv_bonus)
	TextView bonusEdt;

	@InjectView(R.id.rg_gender)
	RadioGroup genderRg;

	@InjectView(R.id.rg_type)
	RadioGroup workingTypeRg;

	Employee employee;
	WorkingMode workingMode;

	DatabaseManager dbManager;

	@Override
	protected int getCurrentViewId() {
		// TODO Auto-generated method stub
		return R.layout.activity_employee_detail;
	}

	@Override
	protected void afterInitView() {
		dbManager = new DatabaseManager(context);

		try {
			if (getIntent().getExtras().containsKey(Define.EMP_EMP_ID)) {

				String empId = getIntent().getStringExtra(Define.EMP_EMP_ID);

				employee = dbManager.getEmployeeById(empId);
				workingMode = dbManager.getWorkingModeByEmpId(empId);

				bindingDataToView();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void bindingDataToView() {
		fullnameEdt.setText(employee.getName());
		codeEdt.setText(employee.getEmpId());
		phoneEdt.setText(employee.getPhone());
		salaryEdt.setText(String.valueOf(workingMode.getSalary()));
		bonusEdt.setText(String.valueOf(workingMode.getBonus()));
		
		codeEdt.setEnabled(false);

		if (employee.getGender() == 0)
			genderRg.check(R.id.male);
		else
			genderRg.check(R.id.female);

		if (workingMode.getWorkingMode() == 0)
			workingTypeRg.check(R.id.fulltime);
		else
			workingTypeRg.check(R.id.parttime);
	}

	public boolean validate() {
		// validate for full name
		if (StringUtils.isEmpty(fullnameEdt)) {
			fullnameEdt.setError(getString(R.string.error_fullname));
			return false;
		} else
			fullnameEdt.setError(null);

		// validate for code
		if (StringUtils.isEmpty(codeEdt)) {
			codeEdt.setError(getString(R.string.error_code));
			return false;
		} else
			codeEdt.setError(null);

		// validate for phone
		if (StringUtils.isEmpty(phoneEdt)) {
			phoneEdt.setError(getString(R.string.error_phone));
			return false;
		} else
			phoneEdt.setError(null);

		// validate for salary
		if (StringUtils.isEmpty(salaryEdt)) {
			salaryEdt.setError(getString(R.string.error_salary));
			return false;
		} else
			salaryEdt.setError(null);

		// validate for bonus
		if (StringUtils.isEmpty(bonusEdt)) {
			bonusEdt.setError(getString(R.string.error_bonus));
			return false;
		} else
			bonusEdt.setError(null);
		
		if(employee == null){
			List<Employee> listEmployees = dbManager.getListEmployees();
			for(Employee emp: listEmployees){
				if(emp.getEmpId().equals(codeEdt.getText().toString())){
					codeEdt.setError(getString(R.string.error_code_duplicate));
					return false;
				}
			}
			
			codeEdt.setError(null);
		}

		return true;
	}

	@OnClick(R.id.bt_save)
	public void onSave() {
		if (validate()) {
			if (employee != null && workingMode != null) {
				// update data for employee
				employee.setName(fullnameEdt.getText().toString().trim());
				employee.setEmpId(codeEdt.getText().toString().trim());
				employee.setPhone(phoneEdt.getText().toString().trim());
				int gender = 0;
				if (genderRg.getCheckedRadioButtonId() == R.id.female) {
					gender = 1;
				}
				employee.setGender(gender);

				dbManager.updateEmployee(employee);

				workingMode.setBonus(Long.parseLong(bonusEdt.getText()
						.toString().trim()));
				workingMode.setSalary(Long.parseLong(salaryEdt.getText()
						.toString().trim()));
				int workingType = 0;
				if (workingTypeRg.getCheckedRadioButtonId() == R.id.parttime) {
					workingType = 1;
				}
				workingMode.setWorkingMode(workingType);

				dbManager.updateWorkingMode(workingMode);

			} else {
				// insert new employee
				employee = new Employee();
				employee.setName(fullnameEdt.getText().toString().trim());
				employee.setEmpId(codeEdt.getText().toString().trim());
				employee.setPhone(phoneEdt.getText().toString().trim());
				int gender = 0;
				if (genderRg.getCheckedRadioButtonId() == R.id.female) {
					gender = 1;
				}
				employee.setGender(gender);

				long empId = dbManager.addNewEmployee(employee);

				if (empId != 0) {
					workingMode = new WorkingMode();
					workingMode.setBonus(Long.parseLong(bonusEdt.getText()
							.toString().trim()));
					workingMode.setSalary(Long.parseLong(salaryEdt.getText()
							.toString().trim()));
					int workingType = 0;
					if (workingTypeRg.getCheckedRadioButtonId() == R.id.parttime) {
						workingType = 1;
					}
					workingMode.setWorkingMode(workingType);
					workingMode.setEmpId(employee.getEmpId());

					dbManager.addNewWorkingMode(workingMode);

				}

			}

			onBackPressed();
		}
	}

}
